package com.itheima.controller;

import com.itheima.domain.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class ImgController {
    @Autowired
    private BookService bookService;
    @GetMapping("/book/image/{id}")
    public void showImage(@PathVariable int id, HttpServletResponse response) throws IOException {
        Book book = bookService.findBookById(id);
        byte[] imageData = book.getFile();
//        System.out.println("图片大小：" + imageData.length);

        if (imageData != null) {
            response.setContentType("image/jpeg"); // 或 image/png
            ServletOutputStream out = response.getOutputStream();
            out.write(imageData);
            out.flush();
            out.close();
        }
    }

}
